//
//  TopTableViewCell.m
//  Ting
//
//  Created by 安静的美男子 on 14/11/12.
//  Copyright (c) 2014年 安静的美男子. All rights reserved.
//

#import "TopTableViewCell.h"
#import "UIButton+WebCache.h"
@implementation TopTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.Scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 150)];
        self.Scroll.pagingEnabled = YES;
        self.Scroll.delegate = self;
        self.Scroll.showsHorizontalScrollIndicator = NO;
        
        [self.contentView addSubview:_Scroll];
        

    }
    return self;
}

-(void)setArr :(NSMutableArray *)arr
{
    
        self.Scroll.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * arr.count, 150);
    if (!_button) {
        //当第一次进来的时候arr.count = 0,所以循环不走第二次进来的时候有值才进入循环,创建赋值
        for (int i = 0; i < arr.count; i++) {
            
            _button = [UIButton buttonWithType:UIButtonTypeCustom];
            _button.frame = CGRectMake(i * [UIScreen mainScreen].bounds.size.width , 0, [UIScreen mainScreen].bounds.size.width, 150);
            [_button sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[arr[i] objectForKey:@"pic"]]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"123.gif"]];
            
            [self.Scroll addSubview:_button];
        }
        _page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 120, [UIScreen mainScreen].bounds.size.width, 30)];
        _page.numberOfPages = arr.count;
        [self.contentView addSubview:_page];
    }
    if (!_time) {
        
        _time = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(change) userInfo:nil repeats:YES];
    }
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
                    self.page.currentPage = self.Scroll.contentOffset.x /[UIScreen mainScreen].bounds.size.width;
}
-(void)change
{
    if (self.Scroll.contentOffset.x /[UIScreen mainScreen].bounds.size.width >=  5) {
        self.Scroll.contentOffset = CGPointMake(0, 0);
                self.page.currentPage = self.Scroll.contentOffset.x /[UIScreen mainScreen].bounds.size.width;
    }else{
    self.Scroll.contentOffset = CGPointMake(_Scroll.contentOffset.x + [UIScreen mainScreen].bounds.size.width , 0);
        self.page.currentPage = self.Scroll.contentOffset.x /[UIScreen mainScreen].bounds.size.width;
        
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
